<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\common\controller\ControllerBase;
/**
 * 前端首页控制器
 */
class Share extends ControllerBase
{
    // 注册页面
    public function register()
    {
        $this->view->engine->layout(false);
        $data=$this->param;
        $info=db('user')->field('id,name')->where('invitation',$data['invitation'])->find();
        if(empty($info)){
            echo json_encode(array('error'=>1,'msg'=>'不存在该用户'));exit;
        }
        $this->assign('info',$info);
        return $this->fetch('register');

    }
    // 注册
    public function registers()
    {
        $data=$this->param;
        $phone=$data['username'];
        $is=db('user')->where('username',$phone)->value('id');
        if(!empty($is)){
            echo json_encode(array('error'=>1,'msg'=>'该手机号已注册！'));exit;
        }
        if(!empty($data['tjid'])){
            $istj=db('user')->where('id',$data['tjid'])->value('id');
            if(empty($istj)){
                echo json_encode(array('error'=>1,'msg'=>'不存在该分享人！'));exit;
            }
        }else{
            $data['tjid']=10000;
        }
        db('user')->startTrans();
        $nocode=db('user')->where("id=".$data['tjid'])->value("nocodea");
        $salt=rand(1000,9999);
        $data['status'] = 1;
        $data['salt'] = $salt;
        $data['phone'] = $phone;
        $data['nocodea'] = $nocode.$data['tjid'].',';
        $data['create_time'] = time();
        $data['invitation']=initcode();
        $data['password']  = data_md5_key($data['password'],$salt);
        $data['passwords']  = data_md5_key($data['password'],$salt);
        if( $msg = $this->checkCodes($phone,$data['code'])){
            echo json_encode(array('error'=>1,'msg'=>$msg));exit;
        }
        $ida=db('user')->insertGetId($data);

        if($ida != false){
            db('user')->commit();
            echo json_encode(array('error'=>0,'msg'=>'注册成功'));exit;
        }else{
            db('user')->rollback();
            echo json_encode(array('error'=>1,'msg'=>'注册失败'));exit;
        }
    }
    /**
     * 验证手机验证码
     */
    private function checkCodes($phone,$code,$type=0)
    {
        $info=db('code')->field('code,create_time')->where("type=".$type." and phone='".$phone."'")->find();
        if($code != $info['code']){
            return '验证码错误';
        }
        if(time()-$info['create_time'] > 180){
            return '验证码已过期';
        }
        return false;
    }
    //首页
    public function index(){
    	$this->view->engine->layout(false);
        if(!$_POST){
            $data=$this->param;
            $uid=$data['uid'];
            session('wid',$data['wid']);
            $this->assign('uid',$uid);
            return $this->fetch('register');
        }else{
            $data=$this->param;
            $phone=$data['username'];
            if( $msg = checkCode($phone,$data['code'],1)){
                echo json_encode($msg);exit;
            }
            $id=db('user')->where('username',$data['username'])->value('id');
            if(empty($id)){
                if(!empty($data['tjid'])){
                    $istj=db('user')->where('id',$data['tjid'])->value('id');
                    if(empty($istj)){
                        echo json_encode(array('code'=>0,'msg'=>'系统繁忙，请稍后重试'));exit;
                    }
                }else{
                    $data['tjid']=10002;
                }
                $nocode=db('user')->where("id=".$data['tjid'])->value("nocodea");
                $data['is']=1;
                $data['status'] = 1;
                $data['phone'] = $phone;
                $data['nocodea'] = $nocode.$data['tjid'].',';
                $data['create_time'] = time();
                $id=db('user')->insertGetId($data);
            }
            session('uid', $id);
            session('tjid',$data['tjid']);
            echo json_encode(array('code'=>0,'msg'=>'注册成功','id'=>$id));exit;
        }
    }
    //此页
    public function userinfo(){
    	$this->view->engine->layout(false);
        if(!$_POST) {
            //session('uid', 10006);
            if (empty(session('uid'))) {
                $this->redirect('index/index');
            }
            $info = db('resume')->where('user_id', session('uid'))->find();
            if (!empty($info['education_id'])) {
                $info['education'] = db('education')->where('education_id', $info['education_id'])->value('name');
            }
            $info['phone']=db('user')->where('id',$info['user_id'])->value('phone');
            $this->assign('info', $info);
            $this->assign('uid', session('uid'));
            return $this->fetch('userinfo');
        }else{
            $data=$this->param;
            if(!empty($data['resume_id'])){
                $data['update_time']=time();
                $data['gzjy']=countages($data['joinTime']);
                $data['age']=countages($data['birthday']);
                $res=db('resume')->where('resume_id',$data['resume_id'])->update($data);
                db('user')->where('id',session('uid'))->update(array('name'=>$data['name']));
                $resume_id=$data['resume_id'];
            }else{
                unset($data['resume_id']);
                $data['create_time']=time();
                $data['update_time']=time();
                $data['status']=1;
                $data['gzjy']=countages($data['joinTime']);
                $data['age']=countages($data['birthday']);
                $data['ismy']=1;
                $res=db('resume')->insertGetId($data);
                db('user')->where('id',session('uid'))->update(array('name'=>$data['name']));
                $resume_id=$res;
            }
            if($msg=$this->deliverResume(session('wid'),session('uid'),$resume_id)){
                echo json_encode(array('code'=>1,'msg'=>$msg));exit;
            }
            if($res != false){
                session('resume_id', $resume_id);
                $eis=db('resume_experience')->where('resume_id',$resume_id)->value('id');
                $edis=db('resume_education')->where('resume_id',$resume_id)->value('id');
                if(!empty($eis) && !empty($edis)){
                    $is=1;
                }else{
                    $is=2;
                }
                echo json_encode(array('code'=>0,'msg'=>'操作成功','is'=>$is));exit;
            }else{
                echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
            }
        }
    }
    //第二页
    public function twoInfo(){
    	$this->view->engine->layout(false);
        if (empty(session('resume_id'))) {
            $this->redirect('index/userinfo');
        }
        if(!$_POST) {
            $info = db('resume')->field('position,position_id')->where('resume_id', session('resume_id'))->find();
            if(!empty($info['position_id'])){
                $info['positionName']=db('position_cat')->where('id',$info['position_id'])->value('catname');
            }
            $this->assign('info', $info);
            return $this->fetch('twoInfo');
        }else{
            $data=$this->param;
            if(empty($data['position'])){
                echo json_encode(array('code'=>1,'msg'=>'请输入求职意向'));exit;
            }
            if($msg=$this->deliverResume(session('wid'),session('uid'))){
                echo json_encode(array('code'=>1,'msg'=>$msg));exit;
            }
            $data['ismy']=1;
            $data['update_time']=time();
            $res=db('resume')->where('resume_id',session('resume_id'))->update($data);
            if($res != false){
                $eis=db('resume_experience')->where('resume_id',session('resume_id'))->value('id');
                $edis=db('resume_education')->where('resume_id',session('resume_id'))->value('id');
                if(!empty($eis) && !empty($edis)){
                    $is=1;
                }else{
                    $is=2;
                }
                echo json_encode(array('code'=>0,'msg'=>'操作成功','is'=>$is));exit;
            }else{
                echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
            }
        }
    }
    /**
     * 简历投递
     * id 职位id
     */
    private function deliverResume($id,$user_id,$resume_id){
        $info=db('work')->field('user_id,status,stat')->where('id',$id)->find();
        if(empty($info)){
            return '不存在该职位';
        }
        if($info['status'] == 0){
            return '该职位招聘已关闭';
        }
        if($info['stat'] == 1){
            return '该职位已停止招聘';
        }
        $create_time=db('deliver_resume')->where(array('user_id'=>$user_id,'work_id'=>$id))->value('create_time');
        if(!empty($create_time) ){
            if( time()-$create_time > 60*60*24*30){
                db("deliver_resume")->where(array('user_id'=>$user_id,'work_id'=>$id))->update(array('create_time'=>time()));
            }else{
                return '您已投递过该职位！';
            }
        }else{
            $is_reward=db('work')->where('id',$id)->value('is_reward');
            if(!empty($is_reward)){
                $data['share_id']=session('tjid');
            }
            $data['is_reward']=$is_reward;
            $data['user_id']=$user_id;
            $data['resume_id']=$resume_id;
            $data['work_id']=$id;
            $data['getid']=$info['user_id'];
            $data['create_time']=time();
            db('deliver_resume')->insertGetId($data);
        }
        //推送
        jpush($data['getid'],6,'有求职者向您发布的职位投递了简历！');
        return false;
    }
    //第三页
    public function experience(){
    	$this->view->engine->layout(false);
        if(empty(session('resume_id'))) {
            $this->redirect('index/userinfo');
        }
        if(!$_POST) {
            $eis=db('resume_experience')->where('resume_id',session('resume_id'))->value('id');
            if(!empty($eis)){
                $this->redirect('share/education');
            }else{
                return $this->fetch('experience');
            }
        }else{
            $data=$this->param;
            if($data['company']=='' && $data['title'] == ''){
                echo json_encode(array('code'=>0,'msg'=>'操作成功'));exit;
            }
            if($data['company'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入公司名称！'));exit;
                return false;
            }
            if($data['title'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入职位名称！'));exit;
                return false;
            }
            if($data['start_time'] == '' || $data['end_time'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入起始时间！'));exit;
                return false;
            }
            if($data['cont'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入工作描述！'));exit;
                return false;
            }
            $data['create_time']=time();
            $data['resume_id']=session('resume_id');
            $res=db('resume_experience')->insertGetId($data);
            if($res != false){
                echo json_encode(array('code'=>0,'msg'=>'操作成功'));exit;
            }else{
                echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
            }
        }
    }
    //教育经历
    public function education(){
    	$this->view->engine->layout(false);
        if(empty(session('resume_id'))) {
            $this->redirect('index/userinfo');
        }
        if(!$_POST) {
            $eis=db('resume_education')->where('resume_id',session('resume_id'))->value('id');
            if(!empty($eis)){
                $this->redirect('index/index');
            }else{
                return $this->fetch('education');
            }
        }else{
            $data=$this->param;
            if($data['school']=='' && $data['education'] == ''){
                echo json_encode(array('code'=>0,'msg'=>'操作成功'));exit;
            }
            if($data['school'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入学校名称！'));exit;
                return false;
            }
            if($data['education'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请选择学历！'));exit;
                return false;
            }
            if($data['start_time'] == '' || $data['end_time'] == ''){
                echo json_encode(array('code'=>1,'msg'=>'请输入起始时间！'));exit;
                return false;
            }
            $data['create_time']=time();
            $data['resume_id']=session('resume_id');
            $res=db('resume_education')->insertGetId($data);
            if($res != false){
                echo json_encode(array('code'=>0,'msg'=>'操作成功'));exit;
            }else{
                echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
            }
        }
    }
    /**
     * 发送短信
     */
    Public function send(){
        $data=$this->param;
        $phone=$data['phone'];
        $is=db('user')->where(array('username'=>$phone,))->value('id');
        if(!empty($is)){
            echo json_encode(array('code'=>1,'msg'=>'该手机号已注册'));exit;
        }
        $code=rand(100000,999999);
        $arr=array('code'=>$code);
        //$response=$this->sendSms($phone,'SMS_173685164',$arr);
        $response=$this->logicShare->sendSms($phone,'SMS_196835009',$arr);
        // $resarr=json_decode( json_encode($response),true);
        if(!$response){
            $is=db('code')->where("phone='".$phone."' and type=0")->value('count');
            if(!empty($is)){
                $data = array(
                    'code'=>$code,
                    'count'=>$is+1,
                    'create_time'=>time()
                );
                db('code')->where(array('phone'=>$phone,'type'=>0))->update($data);
            }else{
                $data = ['code' => $code, 'phone'=>$phone,'create_time' => time(),'type'=>0,'count'=>1];
                db('code')->insert($data);
            }
            echo json_encode(array('code'=>0,'msg'=>'发送成功'));exit;
        }else{
            echo json_encode(array('code'=>1,'msg'=>$response));exit;
        }
    }
    // 首页
    public function sh_job()
    {
    	$this->view->engine->layout(false);
        $data=$this->param;
        $id=$data['wid'];
        $uid=$data['uid'];
        $info=db('work')->where('id',$id)->find();
        if(empty($info)){
            $this->redirect('index/userinfo');
        }
        if($info['status'] == 0){
            $this->assign('msg','职位已关闭');
        }
        if($info['stat'] == 1){
            $this->assign('msg','已停止招聘');
        }
        $info['cont']=preg_replace('/\n/', '<br>', $info['cont']);
        $info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
        $info['experience']=db('experience')->where('experience_id',$info['experience_id'])->value('name');
        if(!empty($info['position_id'])){
            $info['position']=db('position_cat')->where('id',$info['position_id'])->value('catname');
        }
        $cinfo=db('company')->field('company,type,num')->where('user_id',$info['user_id'])->find();
        $cinfo['typeName']=db('company_type')->where('id',$cinfo['type'])->value('name');
        $cinfo['numName']=db('company_num')->where('id',$cinfo['num'])->value('name');
        $cinfo['headimg']=db('user')->where('id',$info['user_id'])->value('headimg');
        $this->assign('info', $info);
        $this->assign('cinfo', $cinfo);
        $this->assign('uid', $uid);
        $this->assign('wid', $id);
        return $this->fetch('jobInfo');
    }
    //简历详情
    public function resumeInfo(){
    	$this->view->engine->layout(false);
        $data=$this->param;
        $data['user_id']=10006;
        $info=db('resume')->where(array('user_id'=>$data['user_id']))->find();
        $info['cont']=preg_replace('/\n/', '<br>', $info['cont']);
        if(!empty($info['position_id'])){
            $info['positionName']=db('position_cat')->where('id',$info['position_id'])->value('catname');
        }
        if(!empty($info['education_id'])){
            $info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
        }
        $info['headimg']=db('user')->where('id',$info['user_id'])->value('headimg');
        $info['age']=countages($info['birthday']);
        $info['gzjy']=countages($info['joinTime']);
        $info['experienceList']=db('resume_experience')->where('resume_id',$info['resume_id'])->select();
        $info['educationList']=db('resume_education')->where('resume_id',$info['resume_id'])->select();
        $this->assign('info', $info);
        return $this->fetch('resumeInfo');
    }
}
